package com.hw01.common;

import com.hw01.entity.Employee;
import com.hw01.vo.LoginUserVo;

/**
 * 获取用户信息，无token验证，这里统一返回 admin
 **/
public class UserInfo {

    // 提供ThreadLocal对象,
    private static final ThreadLocal<LoginUserVo> threadLocal = new ThreadLocal<>();

    public static void set(LoginUserVo user) {
        threadLocal.set(user);
    }

    public static LoginUserVo get() {
        return threadLocal.get();
    }

    // 清除ThreadLocal 防止内存泄漏
    public static void remove() {
        threadLocal.remove();
    }

    public static Long getOrgId() {
        LoginUserVo user = threadLocal.get();
        if (user != null && !"admin".equals(user.getLoginName())) {
            return user.getOrganizationId();
        }
        return null;
    }

    public static Long getUserId() {
        LoginUserVo user = threadLocal.get();
        if (user != null && !"admin".equals(user.getLoginName())) {
            return user.getId();
        }
        return 0L;
    }

    public static String getUserName() {
        LoginUserVo user = threadLocal.get();
        if (user != null && !"admin".equals(user.getLoginName())) {
            return user.getUserName();
        }
        return "admin";
    }

}
